package com.mlnx.push.websocket.server.handler;

import com.alibaba.fastjson.JSON;
import com.mlnx.mptp.ResponseCode;
import com.mlnx.mptp.mptp.head.QoS;
import com.mlnx.mptp.push.PushPacket;
import com.mlnx.mptp.push.body.Body;
import com.mlnx.mptp.push.body.PushDataType;
import com.mlnx.push.websocket.server.protocol.WebPublishMessage;
import com.mlnx.push.websocket.server.session.WebsocketPushNettySession;
import com.mlnx.push.websocket.server.topic.Topic;
import com.mlnx.tp.base.model.DeviceType;

import org.shan.mq.session.core.NettySession;
import org.shan.mq.session.core.Session;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.netty.channel.ChannelHandlerContext;

/**
 * Created by amanda.shan on 2019/12/3.
 */
public class PushHandle extends BaseSimpleChannelInboundHandler<WebPublishMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, WebPublishMessage msg) throws Exception {

        PushPacket pushPacket = msg.getPushPacket();
        Body body = pushPacket.getBody();

        PushPacket sendPacket = null;
        if (body.getMessageId() == null) {
            sendPacket = new PushPacket().pushAck(DeviceType.SERVER, null,
                    ResponseCode.LOST_MESSAGE_ID);
        } else {
            sendPacket = new PushPacket().pushAck(DeviceType.SERVER,
                    body.getMessageId(), ResponseCode.SUCESS);
        }

        Map<PushDataType, Object> pushDataMap = null;
        List<Topic> topics = null;

        WebsocketPushNettySession websocketPushNettySession =
                (WebsocketPushNettySession) getSessionManager().getSession(ctx.channel());

        for (PushDataType type : body.getPushDataMap().keySet()) {
            switch (type) {
                case ADD_DEVICE_LISTEN:
                    topics = JSON.parseArray(body.getPushDataMap().get(type).toString(), Topic.class);
                    websocketPushNettySession.addTopics(topics);

                    pushDataMap = new HashMap<>();
                    pushDataMap.put(PushDataType.ADD_DEVICE_LISTEN, topics);
                    sendPacket.getBody().setPushDataMap(pushDataMap);
                    break;
                case REMOVE_DEVICE_LISTEN:
                    topics = JSON.parseArray(body.getPushDataMap().get(type).toString(), Topic.class);
                    websocketPushNettySession.removeTopics(topics);

                    pushDataMap = new HashMap<>();
                    pushDataMap.put(PushDataType.REMOVE_DEVICE_LISTEN, topics);
                    sendPacket.getBody().setPushDataMap(pushDataMap);
                    break;

                // 推送数据给别的客户端
                case ECG_INFO:
                case SPO_INFO:
                case BP_INFO:
                case TEMP_INFO:
                case CO2_INFO:
                case XMBP_EXELC:
                case WATCH_EXELC:
                case SUGAR_INFO:
                    List<Session> sessions = getSessionManager().getSessions(pushPacket.getBody().getTopic());
                    if (sessions != null && sessions.size() > 0) {

                        for (Session session : sessions) {

                            if (session instanceof NettySession) {
                                NettySession nettySession = (NettySession) session;
                                nettySession.getChannel().writeAndFlush(pushPacket);
                            }
                        }
                    }
                    break;
                default:

            }
        }


        if (sendPacket != null && QoS.LEAST_ONE.equals(msg.getQoS())) {
            ctx.channel().writeAndFlush(sendPacket);
        }
    }
}
